
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BeautyContest.java
 *
 * Created on Feb 25, 2009, 3:13:17 PM
 */

/**
 *
 * @author Vinay
 */
public class RiskPreference extends javax.swing.JFrame {

   int money=10;
   int choice;
   Game g = new Game();

    /** Creates new form BeautyContest */
    public RiskPreference()
    {
        initComponents();
        this.addMouseListener(new RiskPreference.CheckMouse());
    }

    /** Creates new form BeautyContest and sets the parameters*/
    public RiskPreference(int money)
    {
        initComponents();
        this.setVisible(true);
        btnNext.setVisible(false);
        this.money = money;
        this.addMouseListener(new RiskPreference.CheckMouse());
    }

    class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                if(btnNext.isVisible()) {
                    setVisible(false);
                    g.free = true;
                }
                else {
                    int num = Integer.parseInt(lblNum.getText());
                    if(num==6)
                        num=1;
                    else
                        num++;
                    lblNum.setText(""+num);
                }
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {}
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                if(btnNext.isVisible()) {
                    setVisible(false);
                    g.free = true;
                }
                else {
                    getResult();
                }
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblInst = new javax.swing.JLabel();
        btnBet = new javax.swing.JButton();
        lblTitle = new javax.swing.JLabel();
        lblResult = new javax.swing.JLabel();
        btnNext = new javax.swing.JButton();
        btnQuit = new javax.swing.JButton();
        lblNum = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblInst.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblInst.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        lblInst.setText("Select the outcome you want to bet on");

        btnBet.setFont(new java.awt.Font("Tahoma", 0, 14));
        btnBet.setText("Bet");
        btnBet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBetActionPerformed(evt);
            }
        });

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 20));
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTitle.setText("Risk Preference");

        lblResult.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblResult.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        btnNext.setFont(new java.awt.Font("Tahoma", 0, 14));
        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnQuit.setFont(new java.awt.Font("Tahoma", 0, 14));
        btnQuit.setText("Quit");
        btnQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitActionPerformed(evt);
            }
        });

        lblNum.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblNum.setText("1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 710, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(lblInst, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblNum, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(82, Short.MAX_VALUE))
            .addComponent(lblResult, javax.swing.GroupLayout.DEFAULT_SIZE, 710, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(162, Short.MAX_VALUE)
                .addComponent(btnBet, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(123, 123, 123)
                        .addComponent(btnQuit, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(201, 201, 201))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblInst, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNum, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addComponent(lblResult, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBet, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnQuit, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(71, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBetActionPerformed
        // TODO add your handling code here:
        this.getResult();
}//GEN-LAST:event_btnBetActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        g.free=true;
}//GEN-LAST:event_btnNextActionPerformed

    private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitActionPerformed
        // TODO add your handling code here:
    String logText = "RiskPreference, params : Initial Money : $"+money+", Result : Quits";
        g.writeLog(logText);
        lblResult.setText("You decided not to bet");
        btnBet.setVisible(false);
        btnQuit.setVisible(false);
        btnNext.setVisible(true);
}//GEN-LAST:event_btnQuitActionPerformed

    /*Function to run the game logic and display the result*/
    void getResult()
    {
        Random rand = new Random();
        choice = Integer.parseInt(lblNum.getText());

        int outcome = rand.nextInt(5) + 1;

        if(choice == outcome)
        {
            String logText = "RiskPreference, params : Initial Money : $"+money+", Result : Wins";
            g.writeLog(logText);
            lblResult.setText("Congratulations ! You won $ "+money+" !!");
        }
        else
        {
            String logText = "RiskPreference, params : Initial Money : $"+money+", Result : Loses";
            g.writeLog(logText);
            lblResult.setText("Sorry. You Lost.");
        }
        btnBet.setVisible(false);
        btnQuit.setVisible(false);
        btnNext.setVisible(true);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BeautyContest().setVisible(true);
            }
        });
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBet;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnQuit;
    private javax.swing.JLabel lblInst;
    private javax.swing.JLabel lblNum;
    private javax.swing.JLabel lblResult;
    private javax.swing.JLabel lblTitle;
    // End of variables declaration//GEN-END:variables

}
